import {Component, OnInit} from '@angular/core';
import {ExtendHttpService} from "../../common/extend-http.service";
import {TipService} from "../services/tip.service";
import {ActivatedRoute, Router} from "@angular/router";
import {IndexService} from "../services/index.service";
import {Observable} from 'rxjs/Rx';

@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.css']
})
export class SearchComponent implements OnInit {

  // 菜品分类列表
  foodClassList = [];

  // 根据分类取到的菜品列表
  foodList = [];

  allList = new Array();

  // 当前选中的分类
  selectClass = null;

  // 购物车是否显示状态
  shoppingShow = false;

  // 按规格选菜时的显示状态
  standardDialogShow = false;

  // 按规格当前操作的菜品
  foodItem = {};

  // 搜索显示状态
  searchState = true;

  flag = false;

  // 加菜订单id, 加菜时头部桌台变为退出，选桌变为下单
  addFood = {
    addOrderId: '',
    tableClass: '',
    tableName: ''
  }

  tipType = 'error';

  constructor(private extendHttp: ExtendHttpService,
              private tipService: TipService,
              private router: Router,
              private indexService: IndexService,
              private activatedRoute: ActivatedRoute) {
    this.activatedRoute.queryParams.subscribe(param => {
      if (param && param['addOrderId']) {
        this.addFood.addOrderId = param['addOrderId'];
      }
    });
  }


  propertyOp(arr) {
    this.allList = [];
    arr.forEach((v, k) => {
      v.count = 0;
      let count = 0;
      v.foodList.forEach((v1, k1) => {
        count += v1.count;
        this.allList.push(v1);
      });
      v.count = count;
    });

    this.foodClassList = arr;
  }

  goBack() {
    this.router.navigate(['/main/index'],{queryParams: {addOrderId: this.addFood.addOrderId}});
  }

  ngOnInit() {
    cBack =()=> {
      $('#btn-back').trigger('click');
    }
    let search = <HTMLInputElement>document.querySelector('#input_search');
    search.focus();
    let input = Observable.fromEvent(document.querySelector('#input_search'), 'input');
    input.debounceTime(500)
      .map(e => e['target']['value'])
      .subscribe(value => {
        this.foodList = [];
        if (value.trim()) {
          this.allList.forEach(v => {
            if (v.name.includes(value) || v.pinyin.includes(value) || v.pinyin_first.includes(value)) {
              this.foodList.push(v);
            }
          });
        }

        if (this.foodList.length) {
          this.flag = false;
        } else {
          this.flag = true;
        }
      });

    // 进入页面订阅数据
    this.indexService.foodAllEmit.subscribe(res => {
      this.propertyOp(res);
    });

    this.propertyOp(this.indexService.getData());

    this.foodClassList.forEach(v => {
      this.allList.concat(v.foodList);
    });
  }

  /* ---------------------------------------------------------- 按规格操作开始 ---------------------------------------------------------------------- */
  standard(item) {
    this.standardDialogShow = true;
    this.foodItem = item;
  }

  standardShowHide($event) {
    this.standardDialogShow = $event;
  }

  maskShowHide($event) {
    this.standardDialogShow = $event;
  }

  /* ---------------------------------------------------------- 按规格操作结束 ---------------------------------------------------------------------- */

  // 加菜
  add(item) {
    item.count++;
    // this.selectClass['count']++;
    this.indexService.updateItem(item);
  }

  //减菜
  reduce(item) {
    if (item.count > 0) {
      item.count--;
      // this.selectClass['count']--;
      this.indexService.updateItem(item);
    }
  }

  /* ---------------------------------------------------------- 购物车操作开始 ---------------------------------------------------------------------- */

  // 点击购物车图标是否打开购物车列表
  shopNavChange($event) {
    if (typeof $event === "boolean") {
      this.shoppingShow = $event;
    }
  }

  // 点击遮罩关闭
  maskChange($event) {
    this.shoppingShow = $event;
  }

  // 购物车列表没有数据时，关闭
  shopListChange($event) {
    this.shoppingShow = $event;
  }

}
